/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */

var findMedianSortedArrays = function(nums1, nums2) {
  startTempVariable = new Date()
  if (nums1.length > nums2.length) {
    [nums1, nums2] = [nums2, nums1];
    console.log([nums1, nums2])
    // 交换变量 保证第一个 长度 最小
}

const m = nums1.length;
const n = nums2.length;
//m < n
let left = 0;
let right = m;
while (left <= right) {
    const i = Math.floor((left + right) / 2); //  nums1 的中间 1
    const j = Math.floor((m + n + 1) / 2) - i; //  1
    console.log(Math.floor((m + n + 1) / 2) - i)
    console.log(Math.floor(n/2 - 1))
    const maxLeft1 = i === 0 ? -Infinity : nums1[i - 1];
    const minRight1 = i === m ? Infinity : nums1[i];
    const maxLeft2 = j === 0 ? -Infinity : nums2[j - 1];
    const minRight2 = j === n ? Infinity : nums2[j];
    if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
        if ((m + n) % 2 === 0) {
            return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2;
        } else {
            return Math.max(maxLeft1, maxLeft2);
        }
    } else if (maxLeft1 > minRight2) {
        right = i - 1;
    } else {
        left = i + 1;
    }
}
    
  };
  

  
  var a =[1,2,3,6]
  var b = [1,3,4,5,9,10]

  // 3 6 
  // 1 3 4 5 9 10

  //
  var startTempVariable = new Date();
  console.log(findMedianSortedArrays(a,b))
  console.log(startTempVariable)
  console.log(`destructuring: ${new Date() - startTempVariable}ms`);
